﻿using SAO.OracleHelper.ReflectionExtensions;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace SAO.OracleHelper.ValiDateModel
{
    public static class CenterContextExtension
    {
        /// <summary>
        /// 获取枚举描述信息
        /// </summary>
        /// <param name="enum"></param>
        /// <returns></returns>
        public static string GetEnum(this Enum @enum)
        {
            Type type = @enum.GetType();
            foreach (FieldInfo field in type.GetFields())
            {
                if (field.IsDefined(typeof(DataFieldAttribute), true))
                {
                    DataFieldAttribute attribute = field.GetCustomAttribute<DataFieldAttribute>();
                    return attribute.GetName();
                }
            }
            return @enum.ToString();
        }

        /// <summary>
        /// 获取另取表名字段名
        /// </summary>
        /// <param name="member">PropertyInfo继承自MemberInfo</param>
        /// <returns></returns>
        public static string GetName(this MemberInfo member)
        {
            Func<DataAttribute, string> func = new Func<DataAttribute, string>(attribute =>
            {
                if (attribute == null)
                {
                    return $"{member.Name}";
                }
                else
                {
                    return $"{attribute.GetName()}";
                }
            });

            return ActionHanlder(func, member);
        }
        /// <summary>
        /// 获取Key
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public static string GetKey(this Type type)
        {
            foreach (PropertyInfo prop in type.GetProperties())
            {
                if (prop.IsDefined(typeof(KeyAttribute), true))
                {
                    if (prop.IsDefined(typeof(DataAttribute), true))
                    {
                        DataAttribute attribute = prop.GetCustomAttribute<DataAttribute>();
                        return attribute.GetName();
                    }
                    return prop.GetName();
                }
            }
            return null;
        }
        /// <summary>
        /// 返回属性名称大小写
        /// </summary>
        /// <param name="str"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string DataBaseType(this string str, Type type)
        {
            var orStr = str;
            if (type.IsDefined(typeof(DataBaseAttribute), true))
            {
                orStr = str.ToUpper();
            }
            return orStr;
        }
        private static T ActionHanlder<T>(Func<DataAttribute, T> func, MemberInfo member)
        {
            if (member.IsDefined(typeof(DataAttribute), true))
            {
                DataAttribute attribute = member.GetCustomAttribute<DataAttribute>();
                return func.Invoke(attribute);
            }
            return func.Invoke(null);
        }
    }
}
